package com.yx.yinshanjinniu.activitys;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.base.BaseActivity;
import com.yx.yinshanjinniu.bean.RechargeBean;
import com.yx.yinshanjinniu.bean.UserBankCardBean;
import com.yx.yinshanjinniu.net.ActionCallbackListener;
import com.yx.yinshanjinniu.utils.RouteUtil;
import com.yx.yinshanjinniu.utils.TimeCount;
import com.yx.yinshanjinniu.utils.ToastUtil;
import com.yx.yinshanjinniu.utils.Utils;
import com.yx.yinshanjinniu.view.MyEditText;
import com.yx.yinshanjinniu.view.PromptDialog;
import com.yx.yinshanjinniu.view.RechargeDialog;
import com.zc.mychart.utils.FormatUtil;

public class RechargeHasCardActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_card_no, tv_recharge_money, tv_get_code, tv_recharge_number, bt_submit, tv_more_question;
    private RelativeLayout rl_code;
    private MyEditText met_code;
    private UserBankCardBean mCardBean;
    private double mRechargeMoney;
    private String mCode;
    private PromptDialog mUnBindCardDialog;
    private TimeCount timeCount;
    private RechargeBean mRechargeBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mCardBean = (UserBankCardBean) (getIntent().getExtras().getSerializable(RouteUtil.BUNDLE_USER_BANK_MODEL));
        mRechargeMoney = getIntent().getExtras().getDouble(RouteUtil.BUNDLE_RECHARGE_MONEY);
        mCode = getIntent().getExtras().getString(RouteUtil.BUNDLE_RECHARGE_CODE);

        initData();
    }

    private void initData() {
        String rechargeMoney = "充值金额<font color=\"#E74C3C\"> " + FormatUtil.formatDouble2(mRechargeMoney) + " </font>元";
        Utils.setHtmlText(tv_recharge_money, rechargeMoney);
        String cardNo = mCardBean.card_no;
        String cardText = "银行卡（尾号" + cardNo.substring(cardNo.length() - 4) + "）";
        String number = mCardBean.phone;
        tv_recharge_number.setText("预留手机号：" + number);
        tv_card_no.setText(cardText);
    }

    @Override
    protected int setContentView() {
        return R.layout.activity_recharge_has_card;
    }

    @Override
    protected void findViews() {
        tv_card_no = findViewById(R.id.tv_card_no);
        tv_recharge_money = findViewById(R.id.tv_recharge_money);
        rl_code = findViewById(R.id.rl_code);
        met_code = findViewById(R.id.met_code);
        tv_get_code = findViewById(R.id.tv_get_code);
        tv_recharge_number = findViewById(R.id.tv_recharge_number);
        bt_submit = findViewById(R.id.bt_submit);
        tv_more_question = findViewById(R.id.tv_more_question);
    }

    @Override
    protected void setListeners() {
        tv_card_no.setOnClickListener(this);
        tv_get_code.setOnClickListener(this);
        bt_submit.setOnClickListener(this);
        tv_more_question.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v == tv_card_no) {
            showPrePayDialog();
        } else if (v == tv_get_code) {
            if (null != mCardBean && !TextUtils.isEmpty(mCardBean.card_no)) {
                userRecharge();
            }
        } else if (v == bt_submit) {
            userRechargeConfirm();
        } else if (v == tv_more_question) {

        }
    }

    private void showPrePayDialog() {
        mUnBindCardDialog = new PromptDialog.Builder(mContext)
                .setIcon(false)
                .setTitle("解绑银行卡免责声明书")
                .setContent("本人因个人原因需要对本人订购账户所绑定的银行账号进行变更。本人知悉变更后的银行账户必须为本人的银行账户，并且保证能够通过四要素（姓名、银行账号、身份证号、电话号码）验证，否则变更失败的责任由本人自行承担。\n" +
                        "同时，本人自愿自行承担因变更绑定银行账户而对本人造成的一切不利后果。商城对因本人变更绑定银行账户的行为而给本人造成的任何损失与不利影响均免责。")
                .setButton("放弃", "阅读并同意")
                .setGravity(Gravity.LEFT)
                .setOnConfirmListener(new PromptDialog.OnConfirmListener() {
                    @Override
                    public void onConfirm() {
                        unbindCard();
                        mUnBindCardDialog.dismiss();
                    }
                })
                .setOnCancelListener(new PromptDialog.OnCancelListener() {
                    @Override
                    public void onCancel() {
                        mUnBindCardDialog.dismiss();
                    }
                })
                .show();
    }

    private void unbindCard() {
        loading();
        mAppAction.unbindCard(mCode,mCardBean.card_id, new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                cancelLoading();
                ToastUtil.showToast(mContext, "解绑成功");
                backActivity();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                cancelLoading();
                ToastUtil.showToast(mContext, message);
            }

        });
    }

    /**
     * 获取验证码
     */
    private void userRecharge() {
        loading();
        mAppAction.userSaving(mCode, String.valueOf(mRechargeMoney), mCardBean.card_no, mCardBean.phone, mCardBean.card_user_name, mCardBean.cert_no, "",
                new ActionCallbackListener<RechargeBean>() {
                    @Override
                    public void onSuccess(RechargeBean data) {
                        cancelLoading();
                        if (null != data) {
                            timeCount = new TimeCount(tv_get_code, 60000, 1000, "再次获取");
                            timeCount.start();
                            ToastUtil.showToast(mContext, "验证码发送成功");
                            mRechargeBean = data;
                        }
                    }

                    @Override
                    public void onFailure(String errorCode, String message) {
                        cancelLoading();
                        ToastUtil.showToast(mContext, message);
                    }
                });
    }

    /**
     * 确认充值
     */
    private void userRechargeConfirm() {
        String code = met_code.getText();
        if (TextUtils.isEmpty(code)) {
            ToastUtil.showToast(mContext, "请输入验证码");
            return;
        }
        if (null == mRechargeBean) {
            ToastUtil.showToast(mContext, "请先获取验证码");
            return;
        }
        loading();
        mAppAction.userRechargeConfirm("", mRechargeBean.order_id, code, new ActionCallbackListener<Void>() {
            @Override
            public void onSuccess(Void data) {
                cancelLoading();
                RechargeDialog dialog = new RechargeDialog(mContext);
                dialog.init(String.valueOf(mRechargeMoney), new RechargeDialog.DialogListener() {
                    @Override
                    public void onConfirm() {
                        RouteUtil.toMainActivity((BaseActivity) mContext, MainActivity.TAB_HOME);
                    }
                });
                dialog.show();
            }

            @Override
            public void onFailure(String errorCode, String message) {
                cancelLoading();
                ToastUtil.showToast(mContext, message);
            }
        });
    }
}